package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdut.material.pojo.*;
import com.sdut.material.mapper.BuyOrderMapper;
import com.sdut.material.pojo.vo.BuyOrderVO;
import com.sdut.material.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderMapper, BuyOrder> implements IBuyOrderService {
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private ISupplierMaterialService supplierMaterialService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IInputService inputService;
    @Override
    public void insert(Integer materialId,Integer supplierId, Integer demandCount) {
        SupplierMaterial supplierMaterial = supplierMaterialService.selectByMaterialIdAndSupplierId(supplierId,materialId);
        BuyOrder order = new BuyOrder();
        order.setSumaId(supplierMaterial.getId());
        order.setCount(demandCount);
        BigDecimal count = new BigDecimal(0);
        count = BigDecimal.valueOf((int)demandCount);
        order.setAmount(supplierMaterial.getPrice().multiply(count));
        order.setStatus(1);
        order.setShelflife(60);
        buyOrderMapper.insert(order);
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        long offset = (page - 1) * limit;
        List<BuyOrder> buyOrderList = buyOrderMapper.selectByPage(offset,limit);
        int totalCount = buyOrderMapper.selectTotalCount();
        List<BuyOrderVO> buyOrderVOList = new ArrayList<>();
        for (BuyOrder buyOrder : buyOrderList) {
            BuyOrderVO buyOrderVO = new BuyOrderVO();
            buyOrderVO.setId(buyOrder.getId());
            int supplierMaterialId = buyOrder.getSumaId();
            SupplierMaterial supplierMaterial = supplierMaterialService.selectById(supplierMaterialId);
            Material material = materialService.selectById(supplierMaterial.getMaterialId());
            Supplier supplier = supplierService.selectById(supplierMaterial.getSupplierId());
            buyOrderVO.setSupplierName(supplier.getName());
            buyOrderVO.setMaterialName(material.getName());
            buyOrderVO.setCount(buyOrder.getCount());
            buyOrderVO.setAmount(buyOrder.getAmount());
            buyOrderVO.setState(buyOrder.getStatus());
            buyOrderVO.setShelfLife(buyOrder.getShelflife());
            buyOrderVOList.add(buyOrderVO);
        }
        return LayUITableResult.ok((long) totalCount,buyOrderVOList);
    }

    @Override
    public void deleteById(Integer buyOrderId) {
        buyOrderMapper.deleteById(buyOrderId);
    }

    @Override
    public JSONResult check(Integer buyOrderId) {
        BuyOrder buyOrder = buyOrderMapper.selectById(buyOrderId);
        buyOrder.setStatus(2);
        buyOrderMapper.updateById(buyOrder);
        Input input = new Input();
        input.setSumaId(buyOrder.getSumaId());
        input.setCount(buyOrder.getCount());
        input.setStatus(1);
        input.setShelflife(buyOrder.getShelflife());
        inputService.insert(input);
        return JSONResult.ok("审核成功");
    }


}
